package com.edee.bigday.order.listener;

import com.edee.bigday.order.service.OrderService;
import com.edee.common.to.SeckillOrderTo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
@Component
public class OrderSeckillListener {
    @Autowired
    OrderService orderService;

    /**
     * @param seckillOrderTo
     * @param channel     由于消息队列开启了手动确认模式，所以，需要打开通道Channel
     * @param message
     * @throws IOException
     */
    @RabbitHandler
    public void listener(SeckillOrderTo seckillOrderTo, Channel channel, Message message) throws IOException {
        try {
        log.info("准备创建秒杀订单详细信息......");
            orderService.createSeckillOrder(seckillOrderTo);
        } catch (Exception e) {
            //遇到任何异常，拒绝消息并重新入队：
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
